package net.core.common;

import java.util.*;
import java.text.*;

public class DateFormater {
	public final static String INVALIDDATE = "1950-01-01";
	public final static String SQLTYPE = "ORACLE";

	/**
	 * 当前时间 String yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:35:13
	 */
	public final static String getNowTime() {
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter1.format(new Date());
	}

	/**
	 * 今天的日期 yyyy-MM-dd
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:35:44
	 */
	public final static String getToday() {
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
		return formatter1.format(new Date());
	}

	/**
	 * 当前月 yyyyMM
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:36:05
	 */
	public final static String getNowMonth() {
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMM");
		return formatter1.format(new Date());
	}

	/**
	 * 当前月 yyyy-MM
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:44:02
	 */
	public static String getCurMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		return sdf.format(cal.getTime());
	}

	/**
	 * 下月 yyyy-MM
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:43:43
	 */
	public static String getNextMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.MONTH, 1);
		// System.out.println(date.getMonth());
		return sdf.format(cal.getTime());
	}

	/**
	 * 当前年 yyyy
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:36:05
	 */
	public static String getCurYear() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar cal = new GregorianCalendar();
		// cal.setTime(date);
		return sdf.format(cal.getTime());
	}

	/**
	 * @Description: 获得下一年的方法(慎用)
	 * @Title: getNextYear
	 * @param
	 * @param year
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */

	public static String getNextYear(String year) {
		int years = 0;
		try {
			years = Integer.parseInt(year);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return String.valueOf(years + 1);
	}

	/**
	 * 当前月1号的时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:36:19
	 */
	public final static String getMonth01Time() {
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return df.format(calendar.getTime());
	}

	/**
	 * 日期格式化
	 * 
	 * @param formatType
	 *            日期格式
	 * @param formatDate
	 *            日期
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:36:50
	 */
	public final static String getFormatTime(String formatType, java.util.Date formatDate) {
		if (null == formatDate) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
		return formatter.format(formatDate);
	}

	/**
	 * 判断日期字符串(yyyymmdd)是否正确(不是很严禁！没有对日期的30、31、28、29进行判断)
	 * 
	 * @param date_s
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:37:53
	 */
	public final static boolean isRightDate(String date_s) {
		boolean isRight = true;
		if (date_s.length() != 8) {
			isRight = false;
		} else {
			String year_s = date_s.substring(0, 4);
			String month_s = date_s.substring(4, 6);
			String date_ss = date_s.substring(6, 8);
			try {
				int year_i = Integer.parseInt(year_s);
				int month_i = Integer.parseInt(month_s);
				int date_i = Integer.parseInt(date_ss);
				if (year_i < 1000) {
					isRight = false;
				}
				if (month_i < 1 || month_i > 12) {
					isRight = false;
				}
				if (date_i < 1 || date_i > 31) {
					isRight = false;
				}
			} catch (Exception e) {
				isRight = false;
				e.printStackTrace();
			}
		}
		return isRight;
	}

	/**
	 * (1) parameter description
	 * 
	 * @param date
	 * @param type
	 * @param timeInterval
	 * @return (2)function description realize the time plus and minus (3) loop
	 *         condition y: year m: month d: date h:hour f: minute(fenzhong) s:
	 *         second
	 */
	public static Date computeDate(Date date, char type, int timeInterval) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		int Time_year = cal.get(Calendar.YEAR);
		int Time_month = cal.get(Calendar.MONTH);
		int Time_day = cal.get(Calendar.DAY_OF_MONTH);
		int Time_hour = cal.get(Calendar.HOUR_OF_DAY);
		int Time_minute = cal.get(Calendar.MINUTE);
		int Time_second = cal.get(Calendar.SECOND);

		switch (type) {
		case 'y': {
			Time_year = Time_year + timeInterval;
			cal.set(Calendar.YEAR, Time_year);
		}
			break;

		case 'm': {
			Time_month = Time_month + timeInterval;
			cal.set(Calendar.MONTH, Time_month);
		}
			break;

		case 'd': {
			Time_day = Time_day + timeInterval;
			cal.set(Calendar.DAY_OF_MONTH, Time_day);
		}
			break;

		case 'h': {
			Time_hour = Time_hour + timeInterval;
			cal.set(Calendar.HOUR_OF_DAY, Time_hour);
		}
			break;

		case 'f': {
			Time_minute = Time_minute + timeInterval;
			cal.set(Calendar.MINUTE, Time_minute);
		}
			break;

		case 's': {
			Time_second = Time_second + timeInterval;
			cal.set(Calendar.SECOND, Time_second);
		}
			break;
		default:
			break;
		}
		date = cal.getTime();
		return date;
	}

	/**
	 * 根据传入的时间和需计算的差值，获取计算后的时间 算法：通过转化成标准毫秒进行转换。
	 * 
	 * @param dDate
	 *            传入的时间
	 * @param calValue
	 *            需计算的差值
	 * @return 返回字符串
	 * @throws ParseException
	 */
	public final static String getCalDateStr(Date dDate, int calValue) throws ParseException {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long myTime = dDate.getTime() + calValue;
		dDate.setTime(myTime);
		return bartDateFormat.format(dDate);
	}

	/**
	 * 根据传入的时间、类型和需计算的差值，获取计算后的时间 算法：通过转化成标准毫秒进行转换。
	 * 
	 * @param sDate
	 * @param type
	 *            类型，最大粒度到天；目前不支持年月
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */
	public final static String getCalDateStr(String sDate, String type, int calValue) throws ParseException {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int iValue = 0;
		if ("day".equals(type.toLowerCase())) {
			iValue = 24 * 60 * 60 * 1000 * calValue;
		} else if ("hour".equals(type.toLowerCase())) {
			iValue = 60 * 60 * 1000 * calValue;
		} else if ("minute".equals(type.toLowerCase())) {
			iValue = 60 * 1000 * calValue;
		} else if ("second".equals(type.toLowerCase())) {
			iValue = 1000 * calValue;
		}
		return getCalDateStr(bartDateFormat.parse(sDate), iValue);
	}

	/**
	 * 根据传入的时间、类型和需计算的差值，获取计算后的时间 算法：通过转化成标准毫秒进行转换。
	 * 
	 * @param dDate
	 * @param type
	 *            类型，最大粒度到天；目前不支持年月
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */
	public final static String getCalDateStr(Date dDate, String type, int calValue) throws ParseException {
		int iValue = 0;
		if ("day".equals(type.toLowerCase())) {
			iValue = 24 * 60 * 60 * 1000 * calValue;
		} else if ("hour".equals(type.toLowerCase())) {
			iValue = 60 * 60 * 1000 * calValue;
		} else if ("minute".equals(type.toLowerCase())) {
			iValue = 60 * 1000 * calValue;
		} else if ("second".equals(type.toLowerCase())) {
			iValue = 1000 * calValue;
		}
		return getCalDateStr(dDate, iValue);
	}

	/**
	 * 根据传入的时间、类型和需计算的差值，获取计算后的时间 算法：通过转化成标准毫秒进行转换。
	 * 
	 * @param dDate
	 * @param type
	 *            类型，最大粒度到天；目前不支持年月
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */
	public final static Date getCalDateTime(Date dDate, String type, int calValue) throws ParseException {
		int iValue = 0;
		if ("day".equals(type.toLowerCase())) {
			iValue = 24 * 60 * 60 * 1000 * calValue;
		} else if ("hour".equals(type.toLowerCase())) {
			iValue = 60 * 60 * 1000 * calValue;
		} else if ("minute".equals(type.toLowerCase())) {
			iValue = 60 * 1000 * calValue;
		} else if ("second".equals(type.toLowerCase())) {
			iValue = 1000 * calValue;
		}

		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long myTime = dDate.getTime() + iValue;
		dDate.setTime(myTime);
		return dDate;
	}

	/**
	 * get last month 's Month-Time like "2006-03"
	 * 
	 * @param date
	 * @return last month format as yyyy-M
	 */
	public static String getLastMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.MONTH, -1);
		return sdf.format(cal.getTime());
	}

	/**
	 * 根据开始时间和结束时间 获取前一个周期 start=2010-08 end=2010-12 返回应为 [2010-04,2010-08]
	 * 
	 * @param start
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public final static String[] getPreDate(String start, String end) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		cal.setTime(stringToDate(start, "yyyy-MM"));
		int y = cal.get(Calendar.YEAR);
		int m = cal.get(Calendar.MONTH);

		Calendar calEND = new GregorianCalendar();
		calEND.setTime(stringToDate(end, "yyyy-MM"));
		int yE = calEND.get(Calendar.YEAR);
		int mE = calEND.get(Calendar.MONTH);
		int cha = (yE - y) * 12 + (mE - m);

		cal.add(Calendar.MONTH, -cha);
		calEND.add(Calendar.MONTH, -cha);

		return new String[] { sdf.format(cal.getTime()), sdf.format(calEND.getTime()), String.valueOf(cha) };
	}

	/**
	 * 根据开始时间和结束时间 获取前一个周期 start=2010-08 end=2010-12 返回应为 [2010-09,2010-11]
	 * 
	 * @param start
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	public final static String[] getMonthLastDate(String start, String end) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		cal.setTime(stringToDate(start, "yyyy-MM"));

		Calendar calEND = new GregorianCalendar();
		calEND.setTime(stringToDate(end, "yyyy-MM"));

		cal.add(Calendar.MONTH, -1);
		calEND.add(Calendar.MONTH, -1);

		return new String[] { sdf.format(cal.getTime()), sdf.format(calEND.getTime()) };
	}

	/**
	 * String转换Date
	 * 
	 * @param cDate
	 *            日期
	 * @param cFormat
	 *            格式
	 * @return
	 * @throws ParseException
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:40:12
	 */
	public static Date stringToDate(String cDate, String cFormat) throws ParseException {
		SimpleDateFormat formatter1 = new SimpleDateFormat(cFormat);
		return formatter1.parse(cDate);
	}

	/**
	 * 返回14位格式的日期字符串 yyyyMMddHHmmss
	 * 
	 * @param dTime
	 * @return
	 */
	public static String transToNormalString(Date dTime) {
		if (dTime == null) {
			dTime = new Date();
		}
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
		return formatter1.format(dTime);
	}

	/**
	 * 2个日期之间有多少天
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static String daysBetween(String start, String end) throws ParseException {

		Calendar startDate = new GregorianCalendar();
		Calendar endDate = new GregorianCalendar();

		startDate.setTime(stringToDate(start, "yyyy-MM-dd"));
		endDate.setTime(stringToDate(end, "yyyy-MM-dd"));

		Calendar date = (Calendar) startDate.clone();
		long daysBetween = 0;
		while (date.before(endDate)) {
			date.add(Calendar.DAY_OF_MONTH, 1);
			daysBetween++;
		}
		return String.valueOf(daysBetween);
	}

	/**
	 * 日期加月
	 * 
	 * @param date
	 * @param i
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:45:28
	 */
	public static String getAddMonth(String date, int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = new GregorianCalendar();
		try {
			cal.setTime(stringToDate(date, "yyyy-MM"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.MONTH, i);
		// System.out.println(date.getMonth());
		return sdf.format(cal.getTime());
	}

	/**
	 * 日期加 天
	 * 
	 * @param date
	 * @param i
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:45:51
	 */
	public static String getAddDays(String date, int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = new GregorianCalendar();
		try {
			cal.setTime(stringToDate(date, "yyyyMMdd"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.DAY_OF_YEAR, i);
		// System.out.println(date.getMonth());
		return sdf.format(cal.getTime());
	}

	/**
	 * Date 转换为 String（yyyy-MM-dd）
	 * 
	 * @param dDate
	 * @return
	 * @throws ParseException
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:48:06
	 */
	public final static String dateToString(Date dDate) throws ParseException {
		if (null == dDate) {
			return "";
		}
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
		if (formatter1.format(dDate).equals(INVALIDDATE)) {
			return "";
		}
		return formatter1.format(dDate);
	}

	/**
	 * Date 转换为 String（yyyy-MM-dd HH:mm:ss）
	 * 
	 * @param dDate
	 * @return
	 * @throws ParseException
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:47:47
	 */
	public final static String datetimeToString(Date dDate) throws ParseException {
		if (null == dDate) {
			return "";
		}
		if (dateToString(dDate).equals("")) {
			return "";
		}
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter1.format(dDate);
	}

	/**
	 * String 转换为 Date（yyyy-MM-dd）
	 * 
	 * @param cDate
	 * @return
	 * @throws ParseException
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:47:07
	 */
	public final static Date stringToDate(String cDate) throws ParseException {
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
			return formatter1.parse(cDate);
		} catch (ParseException e) {
			try {
				SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return formatter1.parse(cDate);
			} catch (ParseException ee) {
				SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				return formatter1.parse(cDate);
			}
		}
	}

	/**
	 * String 转换为 Date(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param cDate
	 * @return
	 * @throws ParseException
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-31 上午11:46:51
	 */
	public final static Date stringToDateTime(String cDate) throws ParseException {
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return formatter1.parse(cDate);
		} catch (ParseException e) {
			try {
				SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				return formatter1.parse(cDate);
			} catch (ParseException ee) {
				SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
				return formatter1.parse(cDate);
			}
		}
	}
}
